﻿using System;
using System.Windows.Forms;
using mshtml;
using Z.ExtensionMethods.Object;
using Z.GenerateIconProject.WinForm.BL;

namespace Z.GenerateIconProject.WinForm
{
    public partial class MainForm : Form
    {
        public static string Path = @"C:\Users\Jonathan\OneDrive\Documents\ZZZ Projects\Z.IconLibrary\default.html";
        public static string Path2 = @"C:\Users\Jonathan\OneDrive\Documents\ZZZ Projects\Z.IconLibrary\projectSetting.html";

        public HtmlElement doc2;
        Timer timer = new Timer() { Interval = 300, Enabled = true};

        public MainForm()
        {
            
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            dir = dir.PathCombine("ZZZ Projects", "IconLibraryGenerator", "test.xml");

            InitializeComponent();

     
            uiWebBrowser.DocumentCompleted += uiWebBrowser_DocumentCompleted;
            uiWebBrowser.Navigated += uiWebBrowser_Navigated;
            uiWebBrowser.Navigating += uiWebBrowser_Navigating;
     
            uiWebBrowser.Url = new Uri(Path);

            timer.Tick += timer_Tick;


        }

        void timer_Tick(object sender, EventArgs e)
        {
            HtmlElement doc = uiWebBrowser.Document.Body;

            if (uiWebBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                if (doc != doc2)
                {
                    BuildFormProjectSetting(uiWebBrowser);
                }
            }
        }

        void uiWebBrowser_Leave(object sender, EventArgs e)
        {
            
        }

        void uiWebBrowser_Click(object sender, EventArgs e)
        {
            
        }



        private void uiWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.AbsoluteUri == new Uri(Path).AbsoluteUri)
            {
                BuildFormDefault(uiWebBrowser);
            }
            else if (e.Url.AbsoluteUri == new Uri(Path2).AbsoluteUri)
            {
                BuildFormProjectSetting(uiWebBrowser);
            }
        }

        private void uiWebBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (e.Url.AbsoluteUri == new Uri(Path).AbsoluteUri)
            {
                //BuildFormDefault(uiWebBrowser);
            }
        }

        private void uiWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            Uri url = e.Url;

            if (url.AbsoluteUri == new Uri(Path).AbsoluteUri)
            {
                //   BuildFormDefault(uiWebBrowser);
            }
        }

        public void BuildFormDefault(WebBrowser browser)
        {
            HtmlDocument doc = browser.Document;
            doc2 = browser.Document.Body;
            var dom = (IHTMLDocument2) browser.Document.DomDocument;

            if (doc != null)
            {
                var uiProjectCreate = (HTMLButtonElement) doc.GetElementById("uiProjectCreate").DomElement;
                var uiProjectOpen = (HTMLButtonElement) doc.GetElementById("uiProjectOpen").DomElement;
                var uiProjectRecent = (HTMLDivElement) doc.GetElementById("uiProjectRecent").DomElement;

                ApplicationConfiguration applicationConfiguration = ApplicationConfiguration.Load();

                foreach (ApplicationProject project in applicationConfiguration.Projects)
                {
                    IHTMLElement div = dom.createElement("div");
                    div.setAttribute("path", project.ProjectPath);
                    string html = string.Format("<span class='glyphicon glyphicon-file'></span><span class='projectrecent-file'>&nbsp;{0}</span>", project.ProjectName);
                    div.innerHTML = html;

                    ((HTMLElementEvents2_Event) div).onclick += obj =>
                    {
                        string path = project.ProjectPath;

                        ProjectConfiguration projectConfiguration = ProjectConfiguration.Load(path);

                        if (projectConfiguration == null)
                        {
                            MessageBox.Show(@"Could not load the file. The file may be corrupted with invalid data.", @"Corrupted File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        return false;
                    };
                    uiProjectRecent.appendChild((IHTMLDOMNode) div);
                }

                ((HTMLButtonElementEvents2_Event) uiProjectCreate).onclick += ProjectCreate_OnClick;
                ((HTMLButtonElementEvents2_Event) uiProjectOpen).onclick += ProjectOpen_OnClick;
            }
        }

        public bool ProjectCreate_OnClick(IHTMLEventObj pEvtObj)
        {
            uiWebBrowser.Url = new Uri(Path2);
            return false;
        }

        public bool ProjectOpen_OnClick(IHTMLEventObj pEvtObj)
        {
            var dialog = new OpenFileDialog();
            dialog.Filter = @"Icon Project Generator files (*.zicongen)|*.zicongen|All files (*.*)|*.*";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = @"Open a Icon Project Generator";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string path = dialog.FileName;

                ProjectConfiguration projectConfiguration = ProjectConfiguration.Load(path);

                if (projectConfiguration == null)
                {
                    MessageBox.Show(@"Could not load the file. The file may be corrupted with invalid data.", @"Corrupted File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            return false;
        }

        public void BuildFormProjectSetting(WebBrowser browser)
        {
            HtmlDocument doc = browser.Document;
            doc2 = browser.Document.Body;
            var page = new UI.ProjectSetting(uiWebBrowser);
            //var uiAddIconSets = (HTMLButtonElement) doc.GetElementById("uiAddIconSets").DomElement;
            //var uiProjectGenerate = (HTMLButtonElement)doc.GetElementById("uiProjectGenerate").DomElement;

            //((HTMLButtonElementEvents2_Event) uiAddIconSets).onclick += AddIconSets_OnClick;
            //((HTMLButtonElementEvents2_Event)uiProjectGenerate).onclick += ProjectGenerate_OnClick;
        }

        public bool AddIconSets_OnClick(IHTMLEventObj pEvtObj)
        {
            HtmlDocument doc = uiWebBrowser.Document;
            var uiIconSetsTable = (HTMLTable) doc.GetElementById("uiIconSetsTable").DomElement;
            var row = (HTMLTableRow) uiIconSetsTable.insertRow();
            var cell1 = (HTMLTableCell) row.insertCell();
            var cell2 = (HTMLTableCell) row.insertCell();
            var cell3 = (HTMLTableCell) row.insertCell();

            cell1.innerText = "toto1";
            cell2.innerText = "toto2";
            cell3.innerText = "toto3";
            return false;
        }

        public bool ProjectGenerate_OnClick(IHTMLEventObj pEvtObj)
        {
            HtmlDocument doc = uiWebBrowser.Document;

            var uiProjectName = (HTMLInputElement)doc.GetElementById("uiProjectName").DomElement;
            var uiProjectNamespace = (HTMLInputElement)doc.GetElementById("uiProjectNamespace").DomElement;

            var page = new UI.ProjectSetting(uiWebBrowser);
            page.Execute();
            return false;
        }
    }
}